package top.cptr.so.tripartiteqqlogin.utils;

import com.alibaba.fastjson2.annotation.JSONField;
import top.cptr.so.tripartiteqqlogin.nanotation.NoParamFiled;
import top.cptr.so.tripartiteqqlogin.nanotation.ParamFiled;

import java.lang.reflect.Field;

public class ObjectToURIParam {
    public static String toURIParam(Object obj){
        StringBuilder builder = new StringBuilder("?");
        boolean flag=true;
        for (Field field : obj.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            Object value;
            try {
                value = field.get(obj);
                if (value==null || field.isAnnotationPresent(NoParamFiled.class)) continue;
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            String name;
            name=field.getName();
            if (field.isAnnotationPresent(ParamFiled.class)) {
                name = field.getAnnotation(ParamFiled.class).name();
            }
            builder.append("&");
            builder.append(name+"="+(String) value);
        }
        builder.deleteCharAt(1);
        return builder.toString();
    }
}
